import homework9.Bean.School;
import homework9.Bean.Student;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

import javax.persistence.Query;

/**
 * @author CoderXshuai
 */
public class Main {
    private static final SessionFactory FACTORY;

    static {
        //加载核心配置文件
        Configuration configuration = new Configuration();
        configuration.configure();
        //创建SessionFactory对象，并在数据库中把表创建出来
        FACTORY = configuration.buildSessionFactory();
    }

    public static Session getSession() {
        return FACTORY.openSession();
    }

    public static void main(String[] args) {
        final Session session = getSession();
        Query query=session.createQuery("from School ");
        Student student = new Student();
        School primary = new School();
        School middle = new School();
        primary.setSchoolType("小学");
        primary.setSchoolName("绿城小学");
        middle.setSchoolType("中学");
        middle.setSchoolName("实验中学");
        student.setStuName("张三");
        student.getSchoolList().add(primary);
        student.getSchoolList().add(middle);
        primary.setStudent(student);
        middle.setStudent(student);
        session.save(student);
        session.beginTransaction().commit();
    }
}
